﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using qlbansach_thuctapnhom.DAL;
namespace qlbansach_thuctapnhom.BLL
{
   public class loaisach_bll
    {
        quanlybansachDataContext dc = new quanlybansachDataContext();
        //==============================================
        public bool kiemtraloai(string maloai)
        {
            bool ok = dc.loaisaches.Where(t => t.maloai == maloai).FirstOrDefault() != null ? true : false;
            if (!ok)
            {
                return true;
            }
            else return false;
        }
        //==============================================


        //==============================================
        public bool themloai(loaisach obj)
        {
            try
            {
                dc.loaisaches.InsertOnSubmit(obj);
                dc.SubmitChanges();
                return true;
            }
            catch
            { return false; }
        }

        //==============================================
        public bool timkietenloai(string ten)
        {
            bool ok = dc.loaisaches.Where(l => l.ten == ten).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from ls in dc.loaisaches where ls.ten.Equals(ten) select ls;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool timkiemmaloai(string ma)
        {
            bool ok = dc.loaisaches.Where(l => l.maloai == ma).FirstOrDefault() != null ? true : false;
            if (ok)
            {
                var tim = from ls in dc.loaisaches where ls.maloai.Equals(ma) select ls;
                try { return true; }
                catch { return false; }
            }
            else return false;
        }
        //==============================================
        public bool sua(string ma,string ten)
        {
          loaisach  obj = dc.loaisaches.Where(t => t.maloai == ma).FirstOrDefault();
          try
          {
              obj.ten = ten;
              dc.SubmitChanges();
              return true;
          }
          catch { return false; }

        }
       //===============================================
        public bool xoa(string ma)
        {
            loaisach obj = dc.loaisaches.Where(ls => ls.maloai == ma).FirstOrDefault();
            try
            {
                dc.loaisaches.DeleteOnSubmit(obj);
                dc.SubmitChanges();
                return true;
            }
            catch { return false; }
        }
    }
}
